package com.ruoyi.project.tool.swagger.UserController;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ToolUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.annotation.SignatureValidation;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.interceptor.annotation.RepeatSubmit;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysMobileUser;
import com.ruoyi.project.system.domain.SysUserMoney;
import com.ruoyi.project.system.domain.SysUserWithdrawal2;
import com.ruoyi.project.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 用户申请提现Controller
 *
 * @author jjp
 * @date 2020-04-23
 */
@RestController
@Api("用户申请提现Controller")
@RequestMapping("/mobileUser/withdrawal2")
public class UserWithdrawal2Controller extends BaseController {
    @Autowired
    private ISysUserWithdrawal2Service sysUserWithdrawal2Service;
    @Autowired
    private ISysUserMoneyService sysUserMoneyService;
    @Autowired
    private ISysMobileUserService sysMobileUserService;
    @Autowired
    private ISysConfigService sysConfigService;

    /**
     * 查询用户申请提现列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysUserWithdrawal2 sysUserWithdrawal2) {
        sysUserWithdrawal2.setMobileUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        startPage();
        List<SysUserWithdrawal2> list = sysUserWithdrawal2Service.selectSysUserWithdrawal2List(sysUserWithdrawal2);
        return getDataTable(list);
    }

    /**
     * 获取用户申请提现详细信息
     */
    @RepeatSubmit
    @SignatureValidation
    @ApiOperation("获取用户申请提现详细信息")
    @GetMapping(value = "/{withdrawalId}")
    public AjaxResult getInfo(@PathVariable("withdrawalId") Long withdrawalId) {
        return AjaxResult.success(sysUserWithdrawal2Service.selectSysUserWithdrawal2ById(withdrawalId));
    }

    /**
     * 新增用户申请提现
     */
    @Log(title = "用户申请提现", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @SignatureValidation
    @ApiOperation("新增用户申请提现")
    @PostMapping
    public AjaxResult add(@RequestBody SysUserWithdrawal2 sysUserWithdrawal2) {
        String value = (String) sysConfigService.selectConfigByKey("CONFIG_USER_RATIO");
        Double change = Double.valueOf(value);
        BigDecimal exchange = BigDecimal.valueOf(change);
        Long mobileUserId = SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId();
        sysUserWithdrawal2.setMobileUserId(mobileUserId);
        SysMobileUser sysMobileUser = sysMobileUserService.selectSysMobileUserById(mobileUserId);
        //查询用户所有申请提现且在审核状态的钱的总额
        List<SysUserWithdrawal2> list = sysUserWithdrawal2Service.selectSysUserWithdrawal2ListMoney(mobileUserId);
        double withdrawal2Money = 0.0;
        for (int i = 0; i < list.size(); i++) {
            double money = list.get(i).getMoney();
            withdrawal2Money = ToolUtil.add(money, withdrawal2Money);
        }
        //用户余额
        double money1 = sysMobileUser.getMoney();
        if (money1 > ToolUtil.add(withdrawal2Money, sysUserWithdrawal2.getMoney())) {
            BigDecimal money = BigDecimal.valueOf(sysUserWithdrawal2.getMoney());
            double v = sysUserWithdrawal2.getMoney() - money.multiply(exchange).doubleValue();
            sysUserWithdrawal2.setFactMoney(v);
            sysUserWithdrawal2.setWithdrawalStatus("0");
            sysUserWithdrawal2Service.insertSysUserWithdrawal2(sysUserWithdrawal2);
            SysMobileUser mobileUser = new SysMobileUser();
            mobileUser.setMobileUserId(mobileUserId);
            mobileUser.setMoney(ToolUtil.sub(money1, sysUserWithdrawal2.getMoney()));
            sysMobileUserService.updateSysMobileUser(mobileUser);
            return AjaxResult.success();
        }
        return AjaxResult.error("余额不足");
    }


    /**
     * 查询用户余额明细列表
     */
    @PreAuthorize("@ss.hasPermi('system:money:list')")
    @ApiOperation("查询用户余额明细列表")
    @GetMapping("/listUserMoney")
    public TableDataInfo list(SysUserMoney sysUserMoney) {
        sysUserMoney.setMobileUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        startPage();
        List<SysUserMoney> list = sysUserMoneyService.selectSysUserMoneyList(sysUserMoney);
        return getDataTable(list);
    }

}